package com.gdth.bank.model;

import java.util.Date;
import java.util.List;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjBfjl;
import com.gdth.database.bean.TAppBfrjBfjlDao;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjJtcybDao;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppBfrjLxfsDao;
import com.gdth.database.bean.TAppBfrjThjl;
import com.gdth.database.bean.TAppBfrjThjlDao;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppFjxxbDao;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;

import org.greenrobot.greendao.query.QueryBuilder;

import rx.Observable;
import rx.Subscriber;

public class CjkhLxfsModel {
	/**
	 * 查询当前采访客户的所有拜访日记
	 * @param
	 * @return
	 */
	public Observable<List<TAppBfrjLxfs>> getLxfsByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjLxfs>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjLxfs>> arg0) {
				// TODO 查询当前采访客户的所有拜访日记
				TAppBfrjLxfsDao dao = BankApplication.getDaoSession().getTAppBfrjLxfsDao();
				QueryBuilder<TAppBfrjLxfs> qb = dao.queryBuilder();

				qb.where(TAppBfrjLxfsDao.Properties.Jcuuid.like(jcuuid)).orderDesc(TAppBfrjLxfsDao.Properties.Id);

				List<TAppBfrjLxfs> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 获取所有通话记录
	 * @param jcuuid
	 * @return
	 */
	public Observable<List<TAppBfrjThjl>> getThjlByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjThjl>>() {
			
			@Override
			public void call(Subscriber<? super List<TAppBfrjThjl>> arg0) {
				// TODO 获取所有通话记录
				TAppBfrjThjlDao dao = BankApplication.getDaoSession().getTAppBfrjThjlDao();
				QueryBuilder<TAppBfrjThjl> qb = dao.queryBuilder();
				
				qb.where(TAppBfrjThjlDao.Properties.Jcuuid.like(jcuuid)).orderDesc(TAppBfrjThjlDao.Properties.Id);
				
				List<TAppBfrjThjl> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 获取家庭成员
	 * @param jcuuid
	 * @return
	 */
	public Observable<List<TAppBfrjJtcyb>> getJtcyByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjJtcyb>>() {
			
			@Override
			public void call(Subscriber<? super List<TAppBfrjJtcyb>> arg0) {
				// TODO 获取家庭成员
				TAppBfrjJtcybDao dao = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				QueryBuilder<TAppBfrjJtcyb> qb = dao.queryBuilder();
				
				qb.where(TAppBfrjJtcybDao.Properties.Jcuuid.like(jcuuid),
						TAppBfrjJtcybDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete)).orderDesc(TAppBfrjJtcybDao.Properties.Id);
				
				List<TAppBfrjJtcyb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 采访客户基本信息保存
	 * @param mTAppBfrjLxfs
	 * @return
	 */
	public Observable<Long> saveCjkhLxfs(TAppBfrjLxfs mTAppBfrjLxfs){
		final TAppBfrjLxfs tAppBfrjLxfs = mTAppBfrjLxfs;
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 采访客户基本信息保存
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjLxfsDao dao = session.getTAppBfrjLxfsDao();
						
						try {
							dao.insertInTx(tAppBfrjLxfs);
							CJkhxxModel.updateCjkhJbxxZt(tAppBfrjLxfs.getJcuuid());
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(tAppBfrjLxfs.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
	
	/**
	 * 保存通话记录
	 * @param mTAppBfrjThjl
	 * @return
	 */
	public Observable<Long> saveCjkhThjl(TAppBfrjThjl mTAppBfrjThjl, String sfz){
		final TAppBfrjThjl tAppBfrjThjl = mTAppBfrjThjl;
		final String sfzs = sfz;
		return Observable.create(new Observable.OnSubscribe<Long>() {
			
			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 保存通话记录
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {
					
					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjThjlDao dao = session.getTAppBfrjThjlDao();
						TAppBfrjBfjlDao bfdao = session.getTAppBfrjBfjlDao();
						TAppFjxxbDao fjxxDao = session.getTAppFjxxbDao();
						String jcuuid = tAppBfrjThjl.getJcuuid();
						try {
							dao.insertInTx(tAppBfrjThjl);
							
							// 添加拜访记录
							TAppBfrjBfjl mTAppBfrjBfjl = new TAppBfrjBfjl();
							Date date = new Date();
							
							String thjlJcuuid = UUIDUtil.getUUID();
							mTAppBfrjBfjl.setBflx(4);
							mTAppBfrjBfjl.setUuid(thjlJcuuid);
							mTAppBfrjBfjl.setCjsj(date);
							mTAppBfrjBfjl.setBfrq(date);
							mTAppBfrjBfjl.setBbh(1);
							mTAppBfrjBfjl.setSjzt(AppUtil.sjzt_create);
							mTAppBfrjBfjl.setJcuuid(jcuuid);
							

							bfdao.insert(mTAppBfrjBfjl);
							//通话语音保存
							TAppFjxxb mTAppFjxxb = new TAppFjxxb();
							mTAppFjxxb.setAppdz(ToolUtil.getFilePath(tAppBfrjThjl.getLywj()));
							mTAppFjxxb.setFjlx(10);
							mTAppFjxxb.setCjsj(date);
							mTAppFjxxb.setGnmk(1);
							mTAppFjxxb.setSczt(0);
							mTAppFjxxb.setSfz(sfzs);
							if(null != tAppBfrjThjl.getUuid()){
								mTAppFjxxb.setUuid(tAppBfrjThjl.getUuid());
								mTAppFjxxb.setWjmc(tAppBfrjThjl.getUuid() + ".3gp");
							}else{
								if(null !=tAppBfrjThjl.getLywj()){
									String uuid = tAppBfrjThjl.getLywj().replace(".3gp","");
									mTAppFjxxb.setUuid(uuid);
									mTAppFjxxb.setWjmc(uuid + ".3gp");
								}else{
									String uuid = UUIDUtil.getUUID();
									mTAppFjxxb.setUuid(uuid);
									mTAppFjxxb.setWjmc(uuid + ".3gp");
								}
							}
							mTAppFjxxb.setWjlx("3gp");
							mTAppFjxxb.setZdlx(1);
							mTAppFjxxb.setWybs(jcuuid);
							fjxxDao.insert(mTAppFjxxb);
							CJkhxxModel.updateCjkhJbxxZt(jcuuid);
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(tAppBfrjThjl.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
}
